﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace SaleOnlineSystem.Admin.Controls
{
    public partial class ListColorAndSize : System.Web.UI.UserControl
    {
        public int countcolor;
        public int countsize;
        protected void Page_Load(object sender, EventArgs e)
        {
            try
            {
                if (!Page.IsPostBack)
                {
                    //Them du lieu vao cat 1
                    countcolor = 1;
                    rpt_color.DataSource = DAL.DCollector.DBCollector.Instance.StoredProcedureSelectByColumns("tbl_Color", " * ", "selectbycolumns");

                    rpt_color.DataBind();

                    countsize = 1;
                    rpt_size.DataSource = DAL.DCollector.DBCollector.Instance.StoredProcedureSelectByColumns("tbl_Size", " * ", "selectbycolumns");

                    rpt_size.DataBind();

                }
            }
            catch (Exception)
            {
                Response.Redirect("/error.htm");
            }
        }

        protected void btnAddColor_Click(object sender, EventArgs e)
        {
            int nextid = Convert.ToInt32(DAL.DCollector.DBCollector.Instance.getNextID("tbl_Color", "Color_ID"));
            int idnextcolor = nextid + 1;
            DAL.DCollector.DBCollector.Instance.CallStored("callstored", "INSERT INTO tbl_Color VALUES(" + idnextcolor + ",N'" + txtAddColor.Text + "')", false);
            Response.Redirect("/Admin/Index.aspx?panel=1&sub=7");
        }

        protected void btnAddSize_Click(object sender, EventArgs e)
        {
            int nextid = Convert.ToInt32(DAL.DCollector.DBCollector.Instance.getNextID("tbl_Size", "Size_ID"));
            int idnextsize = nextid + 1;
            DAL.DCollector.DBCollector.Instance.CallStored("callstored", "INSERT INTO tbl_Size VALUES(" + idnextsize + ",N'" + txtAddSize.Text + "')", false);
            Response.Redirect("/Admin/Index.aspx?panel=1&sub=7");
        }

        protected void rpt_size_ItemCommand(object source, RepeaterCommandEventArgs e)
        {

            if (e.CommandName == "Edit")
            {
                // Xoa
                int id = Convert.ToInt32(e.CommandArgument.ToString());
                TextBox temp = (TextBox)e.Item.FindControl("txtName");
                temp.Enabled = true;
                LinkButton bt = (LinkButton)e.Item.FindControl("btnEdit");
                bt.Text = "Cập nhật";
                bt.CommandName = "Update";
            }
            if (e.CommandName == "Update")
            {
                // Xoa
                int idsize = Convert.ToInt32(e.CommandArgument.ToString());
                TextBox temp = (TextBox)e.Item.FindControl("txtName");
                string value = temp.Text;

                // Update cai value

                DAL.DCollector.DBCollector.Instance.CallStored("callstored", "Update tbl_Size set Size_Name =N'" + value + "' where Size_ID=" + idsize, false);

                LinkButton bt = (LinkButton)e.Item.FindControl("btnEdit");
                bt.Text = "Sửa";
                bt.CommandName = "Edit";
                temp.Enabled = false;
                Response.Redirect("/Admin/Index.aspx?panel=1&sub=7");
            }

        }

        protected void rpt_color_ItemCommand(object source, RepeaterCommandEventArgs e)
        {
            if (e.CommandName == "UpdateColor")
            {
                int id = Convert.ToInt32(e.CommandArgument.ToString());
                TextBox t = (TextBox)e.Item.FindControl("txtNameColor");
                string colorname = t.Text;
                // Update cai value
                DAL.DCollector.DBCollector.Instance.CallStored("callstored", "Update tbl_Color set Color_Name =N'" + colorname + "' where Color_ID=" + id, false);
                Response.Redirect("/Admin/Index.aspx?panel=1&sub=7");
            }

        }
    }
}